<!--（编辑博客）-->
<template>
  <div>
    <Header></Header>
    <div class="m-content">
      <el-form
        ref="editForm"
        status-icon
        :model="editForm"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="标题" prop="title">
          <el-input v-model="editForm.title"></el-input>
        </el-form-item>
        <el-form-item label="摘要" prop="description">
          <el-input type="textarea" v-model="editForm.description"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <mavon-editor
            ref="md"
            class="content"
            v-model="editForm.content"
            :toolbars="toolbars"
            @imgAdd="$imgAdd"
            :toolbarsBackground="'#f9f9f9'"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm()">立即发表</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import Header from "@/components/Header";
import { mavonEditor } from "mavon-editor";
export default {
  name: "BlogEdit.vue",
  components: { Header },
  data() {
    return {
      toolbars: {
        bold: true, // 粗体
        italic: true, // 斜体
        header: true, // 标题
        underline: true, // 下划线
        strikethrough: true, // 中划线
        mark: false, // 标记
        superscript: false, // 上角标
        subscript: false, // 下角标
        quote: true, // 引用
        ol: true, // 有序列表
        ul: true, // 无序列表
        link: true, // 链接
        imagelink: true, // 图片链接
        code: true, // code
        table: true, // 表格
        fullscreen: true, // 全屏编辑
        readmodel: false, // 沉浸式阅读
        htmlcode: true, // 展示html源码
        help: false, // 帮助
        /* 1.3.5 */
        undo: true, // 上一步
        redo: false, // 下一步
        trash: false, // 清空
        save: false, // 保存（触发events中的save事件）
        /* 1.4.2 */
        navigation: false, // 导航目录
        /* 2.1.8 */
        alignleft: true, // 左对齐
        aligncenter: true, // 居中
        alignright: true, // 右对齐
        /* 2.2.1 */
        subfield: true, // 单双栏模式
        preview: true, // 预览
        boxShadow: false,
        ishljs: true,
      },
      editForm: {
        id: null,
        title: "",
        description: "",
        content: "",
      },
      rules: {
        title: [
          { required: true, message: "请输入标题", trigger: "blur" },
          {
            min: 2,
            max: 100,
            message: "长度在 2 到 100 个字符",
            trigger: "blur",
          },
        ],
        description: [
          { required: true, message: "请输入摘要", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    const blogId = this.$route.params.blogId;
    const _this = this;
    if (blogId) {
      this.$axios.get("/blog/" + blogId).then((res) => {
        const blog = res.data.data;
        _this.editForm.id = blog.id;
        _this.editForm.title = blog.title;
        _this.editForm.description = blog.description;
        _this.editForm.content = blog.content;
      });
    }
  },
  methods: {
    submitForm() {
      const _this = this;
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$axios
            .post("/blog/edit", this.editForm, {
              headers: {
                Authorization: localStorage.getItem("token"),
              },
            })
            .then((res) => {
              _this.$alert("发表成功", "提示", {
                confirmButtonText: "确定",
                callback: (action) => {
                  _this.$router.push("/blogs");
                },
              });
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 绑定@imgAdd event
    $imgAdd(pos, $file) {
      // 第一步.将图片上传到服务器.

      var formdata = new FormData();
      formdata.append("file", $file);
      // 第一步.将图片上传到服务器.
      this.$axios({
        url: "/upload",
        method: "post",
        data: formdata,
      }).then((res) => {
        let _res = res.data;
        console.log(_res.data);
        // 第二步.将返回的url替换到文本原位置![...](0) -> ![...](url)
        // this.$refs.md.$img2Url(pos, _res);
        this.$refs.md.$img2Url(pos, _res.data);
        y;
      });
    },
  },
};
</script>

<style scoped>
.m-content {
  margin: 0 auto;
  /* margin-left: 100px; */
  max-width: 960px;
  text-align: center;
}
.content {
  height: 500px;
  background: rgb(248, 241, 237);
  /* background: rgb(40, 44, 52); */
}
</style>
